package nxu.it.controller;

import io.avaje.http.api.*;
import io.javalin.http.Context;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.ApiResult;
import nxu.it.service.GreetingService;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller("/")
@Singleton
public class MainController {
    @Inject
    GreetingService greetingService;

    @Get("/")
    @Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
    public String index(String name) {
        return "<h1>Yes, it works!</h1>";
    }

    @Get("/hello")
    @Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
    public String hello(String name, Integer gender) {
        String greeting = greetingService.sayHello(name);
        if (gender != null) {
            if (gender == 1) {
                greeting += " 先生";
            } else if (gender == 2) {
                greeting += " 女士";
            }
        }
        return greeting;
    }
    @Get("/login")
    @Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
    public void login(Context ctx) {
        ctx.render("/templates/login.ftl");
    }

    @Post("/loginCheck")
    @Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
    public void loginCheck(Context ctx) {
        String username = ctx.formParam("username");
        String password = ctx.formParam("password");
        if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
            ctx.html("Username and password cannot be empty.");
            return;
        }
        if (username.equals(password)) {
            ctx.html("Login successful!");
        } else {
            ctx.html("Login failed: Incorrect username or password.");
        }
    }



        @Get("/register")
        public void registerForm(Context ctx) {
            ctx.contentType(MediaType.TEXT_HTML + ";charset=UTF-8")
                    .render("/templates/register.ftl");
        }

        @Post("/doRegister")
        public void doRegister(Context ctx) {
            // 获取表单参数
            String username = ctx.formParam("username");
            String password = ctx.formParam("password");
            String gender = ctx.formParam("gender");
            int age = Integer.parseInt(ctx.formParam("age"));
            String origin = ctx.formParam("origin");
            String email = ctx.formParam("email");
            List<String> hobbies = ctx.formParams("hobbies");

            // 输出注册信息
            ctx.contentType(MediaType.TEXT_HTML + ";charset=UTF-8")
                    .html("<h1>注册信息</h1>" +
                            "<p>用户名: " + username + "</p>" +
                            "<p>密码: " + password + "</p>" +
                            "<p>性别: " + gender + "</p>" +
                            "<p>年龄: " + age + "</p>" +
                            "<p>籍贯: " + origin + "</p>" +
                            "<p>邮箱: " + email + "</p>" +
                            "<p>爱好: " + String.join(", ", hobbies) + "</p>");
        }




    @Get("/today")
    public ApiResult today() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        String[] weekdays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        return ApiResult.ok(Map.of("year", year, "month", month, "day", day, "hour", hour, "minute", minute, "weekday", weekdays[weekday - 1]).toString());
    }


    @Get("/json")
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @Get("/render")

    public void render(Context ctx) {
        ctx.contentType(MediaType.TEXT_HTML + ";charset=UTF-8")
                .render("/templates/test.ftl", Map.of("name", "amigo"));

    }
}
